﻿using System;
using System.Threading;
using System.Windows.Forms;
using System.Collections.Generic;
using ElectronicWMSA.Comm.ClassFiles;
using ElectronicWMSA.Comm.Forms.Message;

namespace ElectronicWMSA.Warehouse
{
    public partial class Frm_PanDianOption : Form
    {
        public bool Updateflg = false;
        private int _UpdateSure = 0;
        public Frm_PanDianOption(int UpdateSure, string KW, string JX, string LX, string RQ, string SL)
        {
            InitializeComponent();
            txt_Option_JX.Focus();
            _UpdateSure = UpdateSure;
            txt_Option_KW.Text = KW;
            txt_Option_JX.Text = JX;
            txt_Option_RQ.Text = RQ;
            txt_Option_Num.Text = SL;
            txt_Option_JX.Focus();
        }

        private void btnSaveFP_Click(object sender, EventArgs e)
        {
            try
            {
                if (txt_Option_KW.Text.Trim() == "")
                {
                    MessageClass.error("不能为空！"); txt_Option_KW.Focus(); return;
                }
                if (txt_Option_JX.Text.Trim() == "")
                {
                    MessageClass.error("不能为空！"); txt_Option_JX.Focus(); return;
                }
                if (txt_Option_RQ.Text.Trim() == "")
                {
                    MessageClass.error("不能为空！"); txt_Option_RQ.Focus(); return;
                }
                if (txt_Option_RQ.Text.Trim().Length != 8)
                {
                    MessageClass.error("日期有误！"); txt_Option_RQ.Focus(); return;
                }
                if (txt_Option_Num.Text.Trim() == "")
                {
                    MessageClass.error("不能为空！"); txt_Option_Num.Focus(); return;
                }
                if (!ValidationRegex.ValidteData(txt_Option_Num.Text.Trim()))
                {
                    MessageClass.error("数量出错！"); txt_Option_Num.Focus(); return;
                }
                num = Convert.ToInt32(txt_Option_Num.Text.Trim());
                if (_UpdateSure == 0)
                {
                    if (num == 0)
                    {
                        MessageClass.error("不能为 0 ！"); txt_Option_Num.Focus(); return;
                    }
                    if (MessageClass.enquire("确认保存数据？请仔细排查信息无误后点击确认。\n库位：【" + txt_Option_KW.Text.Trim() + "】   型号：【" + txt_Option_JX.Text.Trim() + "】  盘点日期：【" + txt_Option_RQ.Text.Trim() + "】  数量：【" + txt_Option_Num.Text.Trim() + "】 ") == DialogResult.OK)
                    {
                        WaitingBoxFrm wf = new WaitingBoxFrm((obj, args) => { Thread.Sleep(500); ADD_Option(); }, 60);
                        wf.ShowDialog(this);
                        SystemVariable.AddLogInfo("盘点数据", "操作员：【" + SystemVariable.Login_Name + "修增加灯泡盘点数据，库位：【" + txt_Option_KW.Text.Trim() + "】   型号：【" + txt_Option_JX.Text.Trim() + "】  盘点日期：【" + txt_Option_RQ.Text.Trim() + "】  数量：【" + txt_Option_Num.Text.Trim() + "】 ");
                        Updateflg = true;
                        MessageClass.right("操作完成！");
                        _UpdateSure = 0;
                    }
                }
                else
                {
                    if (MessageClass.enquire("修改数据确认？请仔细排查信息无误后点击确认。\n库位：【" + txt_Option_KW.Text.Trim() + "】   型号：【" + txt_Option_JX.Text.Trim() + "】  盘点日期：【" + txt_Option_RQ.Text.Trim() + "】  数量：【" + txt_Option_Num.Text.Trim() + "】 ") == DialogResult.OK)
                    {
                        string strsql = " DELETE FROM T_Pandian  WHERE PainDian_KW='" + txt_Option_KW.Text.Trim() + "' AND PainDian_machine='" + txt_Option_JX.Text + "' AND BK01='" + txt_Option_RQ.Text + "' AND BK04='" + txt_Option_LX.Text + "'";
                        SYDZ.WMS.DBUtility.DbHelperSQL.ExecuteSql(strsql);
                        SystemVariable.AddLogInfo("盘点数据", "操作员：【" + SystemVariable.Login_Name + "修改灯泡改盘点数据，库位：【" + txt_Option_KW.Text.Trim() + "】   型号：【" + txt_Option_JX.Text.Trim() + "】  盘点日期：【" + txt_Option_RQ.Text.Trim() + "】  数量：【" + txt_Option_Num.Text.Trim() + "】 ");
                        if (num > 0)
                        {
                            WaitingBoxFrm wf = new WaitingBoxFrm((obj, args) => { Thread.Sleep(500); ADD_Option(); }, 60);
                            wf.ShowDialog(this);
                        }

                        Updateflg = true;
                        MessageClass.right("操作完成！");
                        this.Close();
                    }
                }


            }
            catch (Exception ex)
            {
                ErrorLogLoad.ErrorLogInfo(ex.Message.ToString());
                MessageClass.error("操作失败！");
            }
        }
        private int num = 0;
        private void ADD_Option()
        {
            List<String> str = new List<string>();

            string dttime = DateTime.Now.ToString();

            for (int i = 0; i < num; i++)
            {
                str.Add("INSERT INTO T_Pandian  ([PainDian_machine] ,[PainDian_SN]  ,[PainDian_KW]  ,[PainDian_date]  ,[Pandian_name]  ,[Ramak]  ,[Statius]  ,[BK01] ,[BK02]  ,[BK03] ,[BK04] ,[BK05]) VALUES  ('" + txt_Option_JX.Text.Trim() + "','" + Guid.NewGuid().ToString() + "','" + txt_Option_KW.Text.Trim() + "','" + dttime + "','" + SystemVariable.User_Name + "','正常','盘点信息数据','" + txt_Option_RQ.Text.Trim() + "','','盘点完成','Option','')");
            }

            SYDZ.WMS.DBUtility.DbHelperSQL.ExecuteSqlTran(str);
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
